package com.unittec.drainage.provider.auth.mapper;

import java.util.Set;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.unittec.drainage.provider.auth.entity.SysPermission;

/**
 * @description 菜单Mapper
 * @author Jason
 * @date 2019/11/07
 */
public interface SysPermissionMapper extends BaseMapper<SysPermission> {

    /**
     * 修改菜单状态字段： 是否子节点
     *
     * @param id
     * @param leaf
     * @return
     */
    @Update("update sys_permission set is_leaf = #{leaf} where id = #{id}")
    int setMenuLeaf(@Param("id") String id, @Param("leaf") int leaf);

    /**
     * 根据用户id获取菜单
     *
     * @param userId
     * @param platformCode
     * @return
     */
    Set<SysPermission> getSysPermissionByUserId(@Param("userId") String userId,
        @Param("platformCode") String platformCode);

    /**
     * 根据菜单权限编码校验用户是否含有该菜单权限
     *
     * @param userId
     * @param perms
     * @return
     */
    @Select("SELECT COUNT(0) FROM sys_role_permission WHERE permission_id IN (SELECT id FROM sys_permission WHERE FIND_IN_SET( #{perms}, perms )) AND role_id IN (SELECT role_id FROM sys_user_role WHERE user_id = #{userId})")
    int checkPermissionsByPerms(@Param("perms") String perms, @Param("userId") String userId);

    /**
     * 根据菜单权限编码判断菜单是否存在
     *
     * @param perms
     * @return
     */
    @Select("SELECT COUNT(0) FROM sys_permission WHERE FIND_IN_SET( #{perms}, perms ) AND del_flag = '0'")
    int checkPermissionsExistsByPerms(@Param("perms") String perms);

}
